import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../module_common/provider/wallet_info_provider.dart';
import '../../../module_common/router/routers.dart';
import '../../../module_common/utils/auth.dart';
import '../../../module_common/widgets/blockies.dart';
import '../../../module_common/widgets/header/page_header.dart';
import '../../../module_common/widgets/public_tip/public_tip.dart';
import '../widget/textRow.dart';
import '../widgets/sheet/wallet_delete_sheet.dart';
import '../widgets/sheet/wallet_editname_sheet.dart';

//钱包详情页
class WalletDetail extends StatefulWidget {
  const WalletDetail({Key? key}) : super(key: key);

  @override
  _WalletDetailState createState() => _WalletDetailState();
}

class _WalletDetailState extends State<WalletDetail> {
  Wallet? wallet;

  @override
  Widget build(BuildContext context) {
    wallet = context.read<WalletInfoNotifier>().wallet;
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: PageHeaderCom(
          title: String.fromEnvironment('emailString', defaultValue: 'Wallet Details'),
          showBottomBorder: false,
        ),
        body: Column(children: [
          addressWidget(),
          TextRow(
              leftText: 'Wallet Name',
              rightText: context.watch<WalletInfoNotifier>().walletName,
              onTap: () async {
                Utils.showBottomSheetDialog(context, WalletEditNameSheet(wallet: wallet));
              }),
          TextRow(
              leftText: 'Export Private Key',
              onTap: () {
                Authentication()
                    .startLocalAuth(
                        showPasswordError: false,
                        callBackError: () {
                          Toast.show(context,
                              status: '0',
                              position: Dpositon.top,
                              message: "Verification password error",
                              duration: Duration(milliseconds: 3000));
                        })
                    .then((result) {
                  if (result) {
                    Routes.navigateTo(context, Routes.walletExportPrivateKey);
                  }
                });
              }),
          TextRow(
              leftText: 'Node Settings',
              onTap: () {
                Routes.navigateTo(context, Routes.walletSetNode);
              })
        ]),
        bottomNavigationBar: deleteWidget());
  }

  Widget addressWidget() {
    return Padding(
        padding: EdgeInsets.fromLTRB(20, 10, 20, 0),
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(color: "#F6F6F6".toColor()),
            borderRadius: BorderRadius.circular(10.w),
          ),
          height: 60.w,
          child: Row(
            children: [
              SizedBox(width: 15.w),
              UserAvatar(
                walletId: 999 - context.read<WalletInfoNotifier>().walletId,
                size: 40.w,
              ),
              SizedBox(width: 10.w),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(context.watch<WalletInfoNotifier>().walletName,
                      style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.bold, color: "#333333".toColor())),
                  Row(children: [
                    Text(context.watch<WalletInfoNotifier>().walletFormatAddress,
                        style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.bold, color: "#BEBDCA".toColor())),
                    SizedBox(width: 5.w),
                    GestureDetector(
                      onTap: () {
                        //复制
                        Utils.copyText(context.read<WalletInfoNotifier>().walletAddress);
                        Toast.show(context, message: 'Copy successfully');
                      },
                      child: Container(
                        width: 16.w,
                        height: 16.w,
                        // padding: EdgeInsets.only(left: 10, right: 15),
                        child: Image.asset(
                          'assets/images/wallet/wallet_address_copy.png',
                        ),
                      ),
                    )
                  ])
                ],
              ),
            ],
          ),
        ));
  }

  Widget deleteWidget() {
    return Padding(
        padding: EdgeInsets.fromLTRB(28, 0, 28, MediaQuery.of(context).padding.bottom + 20),
        child: GestureDetector(
          child: Container(
            alignment: Alignment.center,
            height: 55.w,
            decoration: BoxDecoration(
              border: Border.all(color: "#FF5100".toColor()),
              borderRadius: BorderRadius.circular(10.w),
            ),
            child: Text("Delete Wallet",
                style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.bold, color: "#FF5100".toColor())),
          ),
          onTap: () {
            //先要弹窗询问
            Utils.showBottomSheetDialog(context, WalletDeleteSheet());
          },
        ));
  }
}
